import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

typedef void PlatformViewControllerCallback(PlatformViewIndicatorController controller);

class PlatformViewIndicatorController {
  PlatformViewIndicatorController._(int id)
      : _channel = MethodChannel('plugins/platforview_indicator_$id');

  final MethodChannel _channel;

  Future<void> start() async {
    return _channel.invokeMethod('start');
  }

  Future<void> stop() async {
    return _channel.invokeMethod('stop');
  }
}

class PlatformViewIndicator extends StatefulWidget {
  const PlatformViewIndicator({
    Key key,
    this.platformViewControllerCallback
  }) : super(key: key);

  final PlatformViewControllerCallback platformViewControllerCallback;

  @override
  State<StatefulWidget> createState() => _PlatformViewIndicatorState();
}

class _PlatformViewIndicatorState extends State<PlatformViewIndicator> {
  @override
  Widget build(BuildContext context) {
    if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: "plugins/platforview_indicator",
        onPlatformViewCreated: _onPlatformViewCreated,
        creationParams: <String, dynamic>{
          "initParams": "初始化参数",
        },
        creationParamsCodec: new StandardMessageCodec(),
      );
    } else if (defaultTargetPlatform == TargetPlatform.android) {
      return AndroidView(
        viewType: "plugins/platforview_indicator",
        onPlatformViewCreated: _onPlatformViewCreated,
        creationParams: <String, dynamic>{
          "initParams": "初始化参数",
        },
        creationParamsCodec: new StandardMessageCodec(),
      );
    }
    return Text('not support now');
  }

  void _onPlatformViewCreated(int id) {
    widget.platformViewControllerCallback?.call(new PlatformViewIndicatorController._(id));
  }
}
